<template>
  <div class="dashboard-container">
    <div class="header">
      <h1>欢迎来到管理后台</h1>
      <div class="user-info">
        <span>当前用户: {{ userStore.username || '未知用户' }}</span>
        <el-button type="primary" @click="handleLogout">退出登录</el-button>
      </div>
    </div>
    
    <div class="content">
      <el-card class="welcome-card">
        <h2>系统概览</h2>
        <p>这是一个基于 Vue3 + TypeScript + Element Plus 的后台管理系统</p>
        <div class="stats">
          <div class="stat-item">
            <h3>用户角色</h3>
            <p>{{ userStore.userInfo?.roles?.join(', ') || '无' }}</p>
          </div>
          <div class="stat-item">
            <h3>权限按钮</h3>
            <p>{{ userStore.userInfo?.buttons?.join(', ') || '无' }}</p>
          </div>
          <div class="stat-item">
            <h3>可访问路由</h3>
            <p>{{ userStore.userInfo?.routes?.join(', ') || '无' }}</p>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../../stores/user'

const router = useRouter()
const userStore = useUserStore()

// 退出登录
const handleLogout = () => {
  userStore.logout()
  router.push('/')
}

// 检查登录状态并获取用户信息
const initPage = async () => {
  // 初始化token（从localStorage读取）
  userStore.initToken()

  if (!userStore.isLoggedIn) {
    router.push('/')
    return
  }

  // 获取用户信息
  const success = await userStore.getUserInfo()
  if (!success) {
    router.push('/')
  }
}

onMounted(() => {
  initPage()
})
</script>

<style scoped lang="scss">
.dashboard-container {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.header {
  background: white;
  padding: 20px 40px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: space-between;
  align-items: center;

  h1 {
    margin: 0;
    color: #333;
    font-size: 24px;
  }

  .user-info {
    display: flex;
    align-items: center;
    gap: 20px;

    span {
      color: #666;
      font-size: 14px;
    }
  }
}

.content {
  padding: 40px;
}

.welcome-card {
  max-width: 800px;
  margin: 0 auto;

  h2 {
    color: #333;
    margin-bottom: 20px;
  }

  p {
    color: #666;
    margin-bottom: 30px;
    line-height: 1.6;
  }
}

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
  margin-top: 30px;
}

.stat-item {
  background: #f8f9fa;
  padding: 20px;
  border-radius: 8px;
  text-align: center;

  h3 {
    margin: 0 0 10px 0;
    color: #333;
    font-size: 16px;
  }

  p {
    margin: 0;
    color: #666;
    font-size: 14px;
  }
}

@media (max-width: 768px) {
  .header {
    padding: 15px 20px;
    flex-direction: column;
    gap: 15px;

    h1 {
      font-size: 20px;
    }
  }

  .content {
    padding: 20px;
  }

  .stats {
    grid-template-columns: 1fr;
  }
}
</style>
